package com.alphathink.deliveryassist.fragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.AlphaThink.DeliveryAssist.R;
import com.alphathink.deliveryassist.eneity.Contact;
import com.alphathink.deliveryassist.eneity.ContactBean;
import com.alphathink.deliveryassist.util.KeyUtil;
import com.google.gson.Gson;
import com.zxing.activity.CaptureActivity;

/**
 * 电话模式界面,扫一扫完成信息读取
 * 
 * @author click
 * 
 */
public class PhoneFragment extends Fragment {

	private String mTitle = "Default";

	public static final String TITLE = "title";
	// 获取布局文件的view
	private View view = null;
	private static final String TAG = "PhoneModelActivity";
	/** 扫描按钮，点击进入扫描二维码 */
	private ImageButton mScanIB, mModelChangeIB, mTemplatesIB;
	/** 收件人,联系方式，收件地址，备注 */
	private TextView mAddresseeTV, mContactTV, mAddressTV, mPhoneModelTipTV;
	/** 拨打电话 */
	private Button mCallBTN;

	private TextView mTitleBarTV;
	/** 为按钮注册监听器 */
	private WigetListener listener = null;

	/** 创建一个快件信息的对象 */
	private ContactBean bean = null;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		if (getArguments() != null) {
			mTitle = getArguments().getString(TITLE);
		}

		view = inflater.inflate(R.layout.activity_phone_fragment_model,
				container, false);
		initView();
		return view;
	}

	void initView() {
		ImageButton mScan = (ImageButton) view
				.findViewById(R.id.IB_scan_phoneModel);

		mAddresseeTV = (TextView) view
				.findViewById(R.id.TV_addresseer_phoneModel);
		mContactTV = (TextView) view.findViewById(R.id.TV_contact_phoneModel);
		mAddressTV = (TextView) view.findViewById(R.id.TV_address_phoneModel);
		mPhoneModelTipTV = (TextView) view.findViewById(R.id.TV_tip_phoneModel);
		mCallBTN = (Button) view.findViewById(R.id.BTN_call_phoneModel);

		listener = new WigetListener();
		mScan.setOnClickListener(listener);
		mCallBTN.setOnClickListener(listener);

	}

	class WigetListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			switch (v.getId()) {
			case R.id.IB_scan_phoneModel:
				Log.d(TAG, "进入电话模式扫描用户信息");
				intent.setClass(getActivity(), CaptureActivity.class);
				startActivityForResult(intent, 0);
				break;
			case R.id.BTN_call_phoneModel:
				Log.d(TAG, "进入到打电话");
				String callNum = mContactTV.getText().toString();
				if (callNum == null || "无".equalsIgnoreCase(callNum)) {
					Toast.makeText(getActivity().getApplicationContext(),
							"请检查联系方式", Toast.LENGTH_SHORT).show();
				} else {
					Intent intent2 = new Intent(Intent.ACTION_DIAL,
							Uri.parse("tel://" + callNum));
					Log.d(TAG, "开始打电话，电话号码是:" + callNum);
					startActivity(intent2);
				}
				break;

			}
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.v(TAG, "扫描返回数据");
		if (resultCode == CaptureActivity.CAP_RESULT) {
			String info = data.getExtras().getString("result");
			Log.e(TAG, "info----" + info);
			String mTmp = KeyUtil.decrypt(info);
			Log.e(TAG, "mTmp----" + mTmp);
			bean = new ContactBean();
			Contact contact = new Contact();
			Gson gson = new Gson();
			try {
				contact = gson.fromJson(mTmp, Contact.class);
				bean = contact.getBeans().get(0);
			} catch (Exception e) {
				Log.e(TAG, e.toString());
			}
			if (bean.getContactNum() == null
					&& bean.getContactAddress() == null
					&& bean.getContactName() == null) {

				AlertDialog.Builder builder = new AlertDialog.Builder(
						getActivity());
				builder.setMessage("信息为空，请重新扫描。").setCancelable(true);
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			} else {
				// mContactTBL.invalidate();此处不用刷新视图，可以保证数据在使用的时候已经赋值
				mAddresseeTV.setText(bean.getContactName());
				mAddressTV.setText(bean.getContactAddress());
				mContactTV.setText(bean.getContactNum());
				mPhoneModelTipTV.setText(bean.getCintactTip());

				Log.d(TAG, "数据已经渲染完毕");
			}
		}

	}
}
